#encoding:utf8
"""
Windows Script Host utils
"""

##########################################################################
def __getwsh(name, instances={}):
	'''
	get a singleton wsh object
	'''

	if name not in instances:
		import win32com.client
		instance	= win32com.client.Dispatch('WScript.%s'%name)
		if not instance:
			raise Exception('Invalid WScript name: %s'%name)
		instances.setdefault(name, instance)
	
	return instances[name]

##########################################################################
def AppActivate(title, wait= False):
	'''
	wsh --> AppActivate
	'''
	__getwsh('shell').AppActivate(str(title), wait)

##########################################################################

def islogin(remotename):
	'''
	check if the pc has login the specific server
	'''
	remotename	= str(remotename)

	if remotename.startswith(r'\\'):
		remotename	= remotename.lower()

		# check if already login
		drives	= __getwsh('network').EnumNetworkDrives()
		for i in range(1, drives.Count(), 2):
			if remotename == drives.Item(i).lower():
				return True
	
	return False

##########################################################################

def login(remotename, username, password):
	'''
	login a share server with the given username and password
	'''
	import pywintypes

	remotename, username, password	= str(remotename), str(username), str(password) 
	if remotename.startswith(r'\\'):
		index	= remotename.find('\\', 2)
		if index!= -1:
			remotename	= remotename[:index]
		#print('remotename= ', remotename, ', username= ', username, 'password= ', password)

		try:
			__getwsh('network').MapNetworkDrive('', remotename, True, username, password)
		except pywintypes.com_error as ex:
			#print(ex)
			pass

##########################################################################

def sendkeys(keys, controls= None): 
	'''
	Send keys to the foregound windows
	'''
	import win32api
	import win32con
	
	if not hasattr(sendkeys, 'shiftkeys'):
		sendkeys.shiftkeys= {'~':'`', '!':'1', '@':'2', '#':'3', '$':'4', '%':'5', '^':'6', '&':'7', '*':'8', '(':'9', ')':'0', '_':'-', '+':'=',
				'{':'[', '}':']', '|':'\\', ':':';', '"':"'", '<':',', '>':'.', '?':'/'}
	
	def putchar(key):
		'''
		put a char 
		'''
		if isinstance(key, int):
			'''
			'key' is a virtual key
			'''
			win32api.keybd_event(key, 0, 0, 0)
			win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)
		elif key in sendkeys.shiftkeys or key>= 'A' and key<= 'Z':
			'''
			'key' is a upper case word 
			'''
			key	= ord(sendkeys.shiftkeys.setdefault(key, key))
			win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0)
			win32api.keybd_event(key, 0, 0, 0)
			win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)
			win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0)
		else:
			'''
			'key' is a lower case word
			'''
			key	= ord(key.upper())
			win32api.keybd_event(key, 0, 0, 0)
			win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)
	
	if not keys:
		raise ValueError('Empty input keys')
	
	keys	= isinstance(keys, int) and (keys, ) or keys
	if not controls:
		'''
		only put out keys
		'''
		for key in keys:
			putchar(key)
	else:
		controls	= isinstance(controls, int) and (controls, ) or controls
		for key in controls:
			win32api.keybd_event(key, 0, 0, 0)
		for key in keys:
			putchar(key)
		for key in reversed(controls):
			win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)
###################################################################################
def trash(path):
	'''
	Move the file into trash bin. 'path' may be a file or a directory
	'''
	import os
	from win32com.shell import shell, shellcon

	if os.path.exists(path):
		ret	= shell.SHFileOperation((0, shellcon.FO_DELETE , path, None, shellcon.FOF_NOCONFIRMATION | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOERRORUI | shellcon.FOF_SILENT))
		return ret[0]==0

	return False

##########################################################################

if __name__=='__main__':
	remotename	= r'\\172.16.4.60\Public\个人\lixianmin01083'
	username	= 'game'
	password	= 'epiegame'
	login(remotename, username, password)
